package edu.uclm.esi.tysw.tusEntradas.persistence;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

import edu.uclm.esi.tysw.tusEntradas.domain.Recintos;
import edu.uclm.esi.tysw.tusEntradas.domain.Usuario;

public class RecintoDAO {
	
	public static void insert(Recintos recinto) throws SQLException {
		Conexion bd = Broker.get().getBD();
		try {
			CallableStatement cs=bd.prepareCall("{call crearRecintos (?, ?, ?)}");
			cs.setString(1,recinto.getNombre());
			cs.setInt(2, recinto.getFilas());
			cs.setInt(3, recinto.getColumnas());
			cs.executeUpdate();
		}
		finally {
			bd.close();
		}
	}
	
	public static ArrayList<String> getAllRecinto() throws SQLException {
		Conexion bd2 = Broker.get().getBD();
		try {
			CallableStatement cs=bd2.prepareCall("{call obtenerNombresRecintos ()}");
						
			ResultSet resultset=cs.executeQuery();

			ArrayList<String> results = new ArrayList<String>();
			ResultSetMetaData metadata = resultset.getMetaData();
			int numcols = metadata.getColumnCount();

			while (resultset.next()) {
			    ArrayList<String> row = new ArrayList<>(numcols); // new list per row
			    int i = 1;
			    while (i <= numcols) {  // don't skip the last column, use <=
			        row.add(resultset.getString(i++));
			    }
			    results.addAll(row);// add it to the result
			}
			return results;
		}
		finally {
			bd2.close();
		}
	}
	public static Recintos getRecinto(String nombre) throws SQLException{
		Conexion bd = Broker.get().getBD();
		Recintos recinto=null;
		try {
			CallableStatement cs=bd.prepareCall("{call obtenerRecinto (?)}");
			cs.setString(1,nombre);
			ResultSet resul = cs.executeQuery();		
			while(resul.next()){
				recinto= new Recintos(resul.getString(1),resul.getInt(2),resul.getInt(3));
			}
		}finally {
			bd.close();
		}
		return recinto;
	}
}
